$(document).ready(function() {
	getEmpTasks("doing_project_task_table","doing_meeting_task_table", 0);
	getEmpTasks("undo_project_task_table","undo_meeting_task_table", -1);
	getEmpTasks("done_project_task_table","done_meeting_task_table", 1);
});

function getEmpTasks(projectDivName, meetingDivName,taskState) {
	$.ajax({
		type: "get",
		projectTasksType: "json",
		async: false,
		cache: false,
		url: "getAllEmpTasks",
		data: {
			taskState: taskState
		},
		success: function(data) {
			console.log(data);
			var projectTasks=data.projectTasks;
			var meetingTasks=data.meetingTasks;

			for(var i = 0; i < projectTasks.length; i++) {
				if(projectTasks[i].taskRealStartTime == null) {
					projectTasks[i].taskRealStartTime = "-";
				} else {
					projectTasks[i].taskRealStartTime = projectTasks[i].taskRealStartTime.replace("T", " ");
				}
				if(projectTasks[i].taskRealEndTime == null) {
					projectTasks[i].taskRealEndTime = "-";
				} else {
					projectTasks[i].taskRealEndTime = projectTasks[i].taskRealEndTime.replace("T", " ");
				}
				if(projectTasks[i].taskPlanStartTime == null) {
					projectTasks[i].taskPlanStartTime = "-";
				} else {
					projectTasks[i].taskPlanStartTime = projectTasks[i].taskPlanStartTime.replace("T", " ");
				}
				if(projectTasks[i].taskPlanEndTime == null) {
					projectTasks[i].taskPlanEndTime = "-";
				} else {
					projectTasks[i].taskPlanEndTime = projectTasks[i].taskPlanEndTime.replace("T", " ");
				}
				var html = "<tr><td>" + (i + 1) + "</td>" +
					"<td>" + projectTasks[i].pmProjectInfo.pname + "</td>" +
					"<td>" + projectTasks[i].taskName + "</td>" +
					"<td>" + projectTasks[i].motusEmpInfoByUserId.name + "</td>" +
					"<td>" + projectTasks[i].taskPlanStartTime + "</td>" +
					"<td>" + projectTasks[i].taskPlanEndTime + "</td>" +
					"<td>" + projectTasks[i].taskRealStartTime + "</td>" +
					"<td>" + projectTasks[i].taskRealEndTime + "</td>" +
					"<td>" + projectTasks[i].taskTotalPoint + "</td>" +
					"</tr>";
				$("#" + projectDivName).append(html);
			}
			
			for(var i = 0; i < meetingTasks.length; i++) {
				if(meetingTasks[i].taskRealStartTime == null) {
					meetingTasks[i].taskRealStartTime = "-";
				} else {
					meetingTasks[i].taskRealStartTime = meetingTasks[i].taskRealStartTime.replace("T", " ");
				}
				if(meetingTasks[i].taskRealEndTime == null) {
					meetingTasks[i].taskRealEndTime = "-";
				} else {
					meetingTasks[i].taskRealEndTime = meetingTasks[i].taskRealEndTime.replace("T", " ");
				}
				if(meetingTasks[i].taskPlanStartTime == null) {
					meetingTasks[i].taskPlanStartTime = "-";
				} else {
					meetingTasks[i].taskPlanStartTime = meetingTasks[i].taskPlanStartTime.replace("T", " ");
				}
				if(meetingTasks[i].taskPlanEndTime == null) {
					meetingTasks[i].taskPlanEndTime = "-";
				} else {
					meetingTasks[i].taskPlanEndTime = meetingTasks[i].taskPlanEndTime.replace("T", " ");
				}
				var html = "<tr><td>" + (i + 1) + "</td>" +
					"<td>" + meetingTasks[i].pmDeptTask.pmMeetingInfo.mname + "</td>" +
					"<td>" + meetingTasks[i].taskName + "</td>" +
					"<td>" + meetingTasks[i].motusEmpInfoByUserId.name + "</td>" +
					"<td>" + meetingTasks[i].taskPlanStartTime + "</td>" +
					"<td>" + meetingTasks[i].taskPlanEndTime + "</td>" +
					"<td>" + meetingTasks[i].taskRealStartTime + "</td>" +
					"<td>" + meetingTasks[i].taskRealEndTime + "</td>" +
					"<td>" + meetingTasks[i].taskTotalPoint + "</td>" +
					"</tr>";
				$("#" + meetingDivName).append(html);
			}
		}
	});

}

function getEmpTasksWithDoingState() {
	$.ajax({
		type: "get",
		projectTasksType: "json",
		async: false,
		cache: false,
		url: "getAllEmpTasks",
		data: {
			taskState: 0
		},
		success: function(projectTasks) {
			console.log(projectTasks);
			for(var i = 0; i < projectTasks.length; i++) {
				if(projectTasks.taskRealStartTime == null) {
					projectTasks[i].taskRealStartTime = "-";
				} else {
					projectTasks[i].taskRealStartTime = projectTasks[i].taskRealStartTime.replace("T", " ");
				}
				if(projectTasks[i].taskRealEndTime == null) {
					projectTasks[i].taskRealEndTime = "-";
				} else {
					projectTasks[i].taskRealEndTime = projectTasks[i].taskRealEndTime.replace("T", " ");
				}
				if(projectTasks[i].taskPlanStartTime == null) {
					projectTasks[i].taskPlanStartTime = "-";
				} else {
					projectTasks[i].taskPlanStartTime = projectTasks[i].taskPlanStartTime.replace("T", " ");
				}
				if(projectTasks[i].taskPlanEndTime == null) {
					projectTasks[i].taskPlanEndTime = "-";
				} else {
					projectTasks[i].taskPlanEndTime = projectTasks[i].taskPlanEndTime.replace("T", " ");
				}
				var html = "<tr><td>" + (i + 1) + "</td>" +
					"<td>" + projectTasks[i].pmProjectInfo.pname + "</td>" +
					"<td>" + projectTasks[i].taskName + "</td>" +
					"<td>" + projectTasks[i].motusEmpInfoByUserId.name + "</td>" +
					"<td>" + projectTasks[i].taskPlanStartTime + "</td>" +
					"<td>" + projectTasks[i].taskPlanEndTime + "</td>" +
					"<td>" + projectTasks[i].taskRealStartTime + "</td>" +
					"<td>" + projectTasks[i].taskRealEndTime + "</td>" +
					"<td>" + projectTasks[i].taskTotalPoint + "</td>" +
					"</tr>";
				$("#doing_task_table").append(html);
			}
		}
	});
}
